//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.study.controller;

import com.study.Util.DataTransformUtil;
import com.study.common.ResultJson;
import com.study.factory.HttpClient;
import com.study.pojo.*;
import com.study.service.*;

import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.stream.events.Comment;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TotalController {
    @Autowired
    private ReadService readService;
    @Autowired
    private BookService bookService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrdersService ordersService;
    @Resource
    private BookPointService bookPointService;
    @Resource
    private CommentsService commentsService;

    public TotalController() {
    }

    @RequestMapping({"/"})
    public String index(HttpSession session, Model model) {
        List<Book> books = this.bookService.getBooks();
        List<Book> rankBooks = this.bookService.getBookByRankList();
        model.addAttribute("books", books);
        model.addAttribute("rankBooks", rankBooks);
        return "index";
    }

    @RequestMapping({"/index/toLogin"})
    public String toLogin() {
        return "login";
    }

    @RequestMapping({"/index/toIndex"})
    public String toIndex() {
        return "redirect:/";
    }

    @RequestMapping({"/index/toRead"})
    public String toRead(Model model) {
        Read content = this.readService.getContent();
        model.addAttribute("read", content);
        return "read";
    }

    @RequestMapping({"/index/toHtml"})
    @ResponseBody
    public void toHtml(String Url, HttpServletResponse response) {
        response.setContentType("image/jpeg;charset=UTF-8");
        String fileName = "a.jpg";
        response.setHeader("Content-Disposition", "inline; filename=\"" + fileName + "\"");
        HttpClient.getURL(Url, response);
    }

    @RequestMapping({"/index/toBookInfo/{bookId}"})
    public String toBookInfo(@PathVariable("bookId") Integer bookId, Model model, HttpServletRequest request) {
        User user = (User)request.getSession().getAttribute("user");
        if (user != null) {
            request.setAttribute("user", user);
        }

        List<Book> chapterBooks = this.bookService.getChaptersByBookId(bookId);
        List<Book> rankBooks = this.bookService.getBookRankByType(((Book)chapterBooks.get(0)).getType());
        model.addAttribute("bookId", bookId);
        model.addAttribute("rankBooks", rankBooks);
        model.addAttribute("chapterBooks", chapterBooks.get(0));
        double allPoints = 0.0D;
        List<BookPoint> bookPoints = this.bookPointService.queryAllPoints(bookId);
        if (bookPoints.size() > 0) {
            BookPoint bookPoint;
            for(Iterator var10 = bookPoints.iterator(); var10.hasNext(); allPoints += Double.parseDouble(bookPoint.getNumber())) {
                bookPoint = (BookPoint)var10.next();
            }

            model.addAttribute("allPoints", DataTransformUtil.SaveTwoDecimal(allPoints * 1.8D / (double)bookPoints.size()));
            model.addAttribute("pointValue", DataTransformUtil.SaveTwoDecimal(allPoints / (double)bookPoints.size()));
        } else {
            model.addAttribute("allPoints", 5.0D);
            model.addAttribute("pointValue", 5.0D);
        }

        //获取图书评论
        List<Comments> comments = commentsService.queryAll(new Comments(bookId));
        if (comments.size() > 0){
            request.setAttribute("comments",comments);
        }
        return "bookInfo";
    }

    @RequestMapping({"/index/toNovel/{bookType}"})
    public String toNovel(@PathVariable int bookType, Model model) {
        List<Book> novelBooks = null;
        List<Book> rankBooks = null;
        Integer counts = null;
        switch(bookType) {
            case 1:
                novelBooks = this.bookService.getBookByPage(1, 30, "小说作品");
                counts = this.bookService.getCountByType("小说作品");
                rankBooks = this.bookService.getBookRankByType("小说作品");
                model.addAttribute("rankBooks", rankBooks);
                model.addAttribute("counts", counts);
                model.addAttribute("novelBooks", novelBooks);
                return "novel";
            case 2:
                novelBooks = this.bookService.getBookByPage(1, 30, "经济管理");
                counts = this.bookService.getCountByType("经济管理");
                rankBooks = this.bookService.getBookRankByType("经济管理");
                model.addAttribute("rankBooks", rankBooks);
                model.addAttribute("counts", counts);
                model.addAttribute("novelBooks", novelBooks);

                return "economy";
            case 3:

                novelBooks = this.bookService.getBookByPage(1, 30, "历史传记");
                counts = this.bookService.getCountByType("历史传记");
                rankBooks = this.bookService.getBookRankByType("历史传记");
                model.addAttribute("rankBooks", rankBooks);
                model.addAttribute("counts", counts);
                model.addAttribute("novelBooks", novelBooks);
                return "history";
            case 4:
                novelBooks = this.bookService.getBookByPage(1, 30, "生活时尚");
                counts = this.bookService.getCountByType("生活时尚");
                rankBooks = this.bookService.getBookRankByType("生活时尚");
                model.addAttribute("rankBooks", rankBooks);
                model.addAttribute("counts", counts);
                model.addAttribute("novelBooks", novelBooks);
                return "life";
            case 5:
                novelBooks = this.bookService.getBookByPage(1, 30, "文学艺术");
                counts = this.bookService.getCountByType("文学艺术");
                rankBooks = this.bookService.getBookRankByType("文学艺术");
                model.addAttribute("rankBooks", rankBooks);
                model.addAttribute("counts", counts);
                model.addAttribute("novelBooks", novelBooks);
                return "literature";
            case 6:
                novelBooks = this.bookService.getBookByPage(1, 30, "社会科学");
                counts = this.bookService.getCountByType("社会科学");
                rankBooks = this.bookService.getBookRankByType("社会科学");
                model.addAttribute("rankBooks", rankBooks);
                model.addAttribute("counts", counts);
                model.addAttribute("novelBooks", novelBooks);
                return "society";
            case 7:
                novelBooks = this.bookService.getBookByPage(1, 30, "成功励志");
                counts = this.bookService.getCountByType("成功励志");
                rankBooks = this.bookService.getBookRankByType("成功励志");
                model.addAttribute("rankBooks", rankBooks);
                model.addAttribute("counts", counts);
                model.addAttribute("novelBooks", novelBooks);
                return "success";
            default:
                return "redirect:/";
        }
    }

    @RequestMapping({"/index/doNovel/{bookType}"})
    @ResponseBody
    public ResultJson doNovel(@PathVariable int bookType, @RequestParam("page") int page, Model model) {
        List<Book> novelBooks = null;
        switch(bookType) {
            case 1:
                novelBooks = this.bookService.getBookByPage(page, 30, "小说作品");
                model.addAttribute("novelBooks", novelBooks);
                break;
            case 2:
                novelBooks = this.bookService.getBookByPage(page, 30, "经济管理");
                model.addAttribute("novelBooks", novelBooks);
                break;
            case 3:
                novelBooks = this.bookService.getBookByPage(page, 30, "历史传记");
                model.addAttribute("novelBooks", novelBooks);
                break;
            case 4:
                novelBooks = this.bookService.getBookByPage(page, 30, "生活时尚");
                model.addAttribute("novelBooks", novelBooks);
                break;
            case 5:
                novelBooks = this.bookService.getBookByPage(page, 30, "文学艺术");
                model.addAttribute("novelBooks", novelBooks);
                break;
            case 6:
                novelBooks = this.bookService.getBookByPage(page, 30, "社会科学");
                model.addAttribute("novelBooks", novelBooks);
                break;
            case 7:
                novelBooks = this.bookService.getBookByPage(page, 30, "成功励志");
                model.addAttribute("novelBooks", novelBooks);
        }

        return ResultJson.ok(novelBooks);
    }

    @RequestMapping({"index/toSearch"})
    public String toSearch(@RequestParam("name") String name, Model model) {
        List<Book> books = this.bookService.getBookByLike(name, 1, 8);
        Integer count = this.bookService.getCountByLikes(name);
        List<Book> bookByRankList = this.bookService.getBookByRankList();
        model.addAttribute("books", books);
        model.addAttribute("counts", count);
        model.addAttribute("name", name);
        model.addAttribute("rankBooks", bookByRankList);
        return "search";
    }

    @RequestMapping({"index/doSearch"})
    @ResponseBody
    public ResultJson doSearch(@RequestParam("name") String name, @RequestParam("page") int page, Model model) {
        List<Book> books = this.bookService.getBookByLike(name, page, 8);
        Integer count = this.bookService.getCountByLikes(name);
        model.addAttribute("books", books);
        model.addAttribute("counts", count);
        return ResultJson.ok(books);
    }

    @RequestMapping({"/index/toAccount/{phone}"})
    public String toAccount(@PathVariable String phone, Model model) {
        User user = this.userService.findByPhone(phone);
        model.addAttribute("user", user);
        return "accountInfo";
    }

    @RequestMapping({"/index/doUpdateAccount"})
    @ResponseBody
    public ResultJson doUpdateAccountInfo(User user) {
        this.userService.updateUser(user);
        return ResultJson.ok();
    }

    @RequestMapping({"/index/toBookshelf/{userId}"})
    public String toMyBookshelf(@PathVariable Integer userId, Model model) {
        if (userId == 0) {
            return "bookshelf";
        } else {
            List<Orders> books = this.ordersService.getMyOrdersBook(userId, 1);
            List<Book> bookByRankList = this.bookService.getBookByRankList();
            model.addAttribute("books", books);
            model.addAttribute("rankBooks", bookByRankList);
            model.addAttribute("counts", books.size());
            return "bookshelf";
        }
    }

    @RequestMapping({"/index/nev"})
    public String toNev(HttpSession session) {
        return "nev";
    }

    @RequestMapping({"/index/exit"})
    public String exitSession(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/";
    }
}
